package cn.kgc.vue.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.util.ObjectUtil;
import cn.kgc.vue.anno.LogAnnotation;
import cn.kgc.vue.common.Operation;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.StudentPageDTO;
import cn.kgc.vue.entity.Student;
import cn.kgc.vue.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: 课工场
 * @since 2023/8/21
 */
@RequestMapping("student")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;


    //获取专业人数 大屏展示
    @GetMapping("screenData")
    public ResponseResult getScreenData(){
        return   studentService.getScreenData();
    }

    // 批量导入    batchAdd
    @PostMapping("batchAdd")
    @RequiresPermissions("base:student:add")
    public ResponseResult batchAdd(@RequestParam("file") MultipartFile file) throws Exception {
        return studentService.batchAdd(file);
    }

    // 添加学生信息  addOrUpdateStudent
    @PostMapping("addOrUpdate")
    @LogAnnotation(model = "学生模块",operation = Operation.ADD)
    @RequiresPermissions(value = {"base:student:add","base:student:update"},logical = Logical.OR)
    public ResponseResult addOrUpdate(@RequestBody Student student) {

        ResponseResult responseResult = new ResponseResult();

        String studentNo = student.getStudentNo();
        LambdaQueryWrapper<Student> lambda = new QueryWrapper<Student>().lambda();
        lambda.eq(Student::getStudentNo,student.getStudentNo())
        .eq(ObjectUtil.isNotNull(student.getId()),Student::getClazz,student.getClazz())
        .eq(ObjectUtil.isNotNull(student.getId()),Student::getName,student.getName())
        .eq(ObjectUtil.isNotNull(student.getId()),Student::getAge,student.getAge())
        .eq(ObjectUtil.isNotNull(student.getId()),Student::getTel,student.getTel())
        .eq(ObjectUtil.isNotNull(student.getId()),Student::getCourseId,student.getCourseId())
        .eq(ObjectUtil.isNotNull(student.getId()),Student::getClazz,student.getClazz())
        .eq(ObjectUtil.isNotNull(student.getId()),Student::getSchoolYear,student.getSchoolYear());

        Student stu = studentService.getOne(lambda);
        if (ObjectUtil.isNotEmpty(stu)){
            responseResult.fail(ResponseStatus.STUDENTNO_EXIST);
            return responseResult;
        }

        studentService.saveOrUpdate(student);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }


    //根据id获取学生信息
    @GetMapping("getById")
    public ResponseResult getById(Integer id) {
        ResponseResult responseResult = new ResponseResult();
        Student student = studentService.getById(id);
        responseResult.success(ResponseStatus.SUCCESS,student);
        return responseResult;
    }

    //根据id删除学生信息
    @DeleteMapping("delete")
    @LogAnnotation(model = "学生模块",operation = Operation.DELETE)
    @RequiresPermissions(value = {"base:student:delete"})
    public ResponseResult deleteById(Integer id) {
        ResponseResult responseResult = new ResponseResult();
        studentService.removeById(id);
        responseResult.success(ResponseStatus.SUCCESS);
        return responseResult;
    }

    @GetMapping("page")
    public ResponseResult getPage(StudentPageDTO studentPageDTO) {
        return studentService.getStudentPage(studentPageDTO);
    }

    @GetMapping("excel")
    public void exportExcel(HttpServletResponse response, String studentNo, String clazz) throws IOException {

        LambdaQueryWrapper<Student> lambda = new QueryWrapper<Student>().lambda();
        lambda.eq(StringUtils.isNotBlank(studentNo), Student::getStudentNo, studentNo)
                .eq(StringUtils.isNotBlank(clazz), Student::getClazz, clazz);
        List<Student> students = studentService.list(lambda);

        ExportParams exportParams = new ExportParams("学生信息表", "学生信息");

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Student.class, students);
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();
    }
}
